﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Oil
    {
        public Oil()
        { }
        public int Add(Model.Oil model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Km", SqlDbType.BigInt),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Addr", SqlDbType.NVarChar,100),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Num", SqlDbType.Int,4),
					new SqlParameter("@Heji", SqlDbType.Decimal,9),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime)};
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = model.CarCode;
            parameters[2].Value = model.Km;
            parameters[3].Value = model.Time;
            parameters[4].Value = model.Addr;
            parameters[5].Value = model.Price;
            parameters[6].Value = model.Num;
            parameters[7].Value = model.Heji;
            parameters[8].Value = model.Marks;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.InputTime;
            int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Oil_ADD", parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Oil model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@Km", SqlDbType.BigInt),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Addr", SqlDbType.NVarChar,100),
					new SqlParameter("@Price", SqlDbType.Decimal,9),
					new SqlParameter("@Num", SqlDbType.Int,4),
					new SqlParameter("@Heji", SqlDbType.Decimal,9),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.CarCode;
            parameters[2].Value = model.Km;
            parameters[3].Value = model.Time;
            parameters[4].Value = model.Addr;
            parameters[5].Value = model.Price;
            parameters[6].Value = model.Num;
            parameters[7].Value = model.Heji;
            parameters[8].Value = model.Marks;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.InputTime;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Oil_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Oil_Delete", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        public Model.Oil GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Oil model = new Model.Oil();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Oil_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }

                model.CarCode = ds.Tables[0].Rows[0]["CarCode"].ToString();
                
                if (ds.Tables[0].Rows[0]["Km"].ToString() != "")
                {
                    model.Km = int.Parse(ds.Tables[0].Rows[0]["Km"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Time"].ToString() != "")
                {
                    model.Time = DateTime.Parse(ds.Tables[0].Rows[0]["Time"].ToString());
                }
                model.Addr = ds.Tables[0].Rows[0]["Addr"].ToString();
                if (ds.Tables[0].Rows[0]["Price"].ToString() != "")
                {
                    model.Price = decimal.Parse(ds.Tables[0].Rows[0]["Price"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Num"].ToString() != "")
                {
                    model.Num = int.Parse(ds.Tables[0].Rows[0]["Num"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Heji"].ToString() != "")
                {
                    model.Heji = decimal.Parse(ds.Tables[0].Rows[0]["Heji"].ToString());
                }
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                if (ds.Tables[0].Rows[0]["InputUserId"].ToString() != "")
                {
                    model.InputUserId =int.Parse(ds.Tables[0].Rows[0]["InputUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputTime"].ToString() != "")
                {
                    model.InputTime = DateTime.Parse(ds.Tables[0].Rows[0]["InputTime"].ToString());
                }
                model.InputTrueName = ds.Tables[0].Rows[0]["TrueName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,CarCode,Km,Time,Addr,Price,Num,Heji,Marks,InputUserId,InputTime ");
            strSql.Append(" FROM Oil ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }

    }
}
